﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.Regions;
using TimeSplicerModule.DomainModels;
using TimeSplicerModule.ViewModels;
using TimeSplicerModule.Factories;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;
using TimeSplicerModule.Controllers;
using TimeSplicerModule.Services;

namespace TimeSplicerModule.Views
{
    /// <summary>
    /// Interaction logic for UserView.xaml
    /// </summary>
    public partial class UserView : UserControl, INavigationAware
    {
        private UserViewModel Model;
        private readonly IUnityContainer _container;
        private readonly IUserController _userController;
        private readonly IUserService _userService;
        private readonly IUserFactory _userFactory;
        private readonly IStatusFactory _statusFactory;
        private readonly IApplicationRoleFactory _applicationRoleFactory;
        private readonly ITeamFactory _teamFactory;

        public UserView(IUnityContainer container, IUserController userController, IUserService userService, IUserFactory userFactory, 
            IStatusFactory statusFactory, IApplicationRoleFactory applicationRoleFactory, ITeamFactory teamFactory)
        {
            InitializeComponent();

            _container = container;
            _userController = userController;
            _userService = userService;
            _userFactory = userFactory;
            _statusFactory = statusFactory;
            _applicationRoleFactory = applicationRoleFactory;
            _teamFactory = teamFactory;
        }

        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return false; //throw new NotImplementedException();
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            //throw new NotImplementedException();
        }

        public async void OnNavigatedTo(NavigationContext navigationContext)
        {
            //var statuses = _statusFactory.GetList(x => x.IsUserStatus);
            //var applicationRoles = _applicationRoleFactory.GetList();
            //var teams = _teamFactory.GetList();
            //var jobRoles = _jobRoleFactory.GetList();


            if (navigationContext.Parameters.Count() == 0)
            {
                await Task.Run(() => Model = _userService.CreateNew());
                //Model = _userFactory.CreateNew(statuses, applicationRoles, teams, jobRoles);
            }
            else
            {
                string id = navigationContext.Parameters["id"].ToString();
                Guid result;

                if (Guid.TryParse(id, out result))
                {
                    await Task.Run(() => Model = _userService.Get(result));
                   // Model = _userFactory.Get(x => x.UserGuid == result, statuses, applicationRoles, teams, jobRoles);
                }
            }


            this.DataContext = Model;
        }

        private async void saveButton_Click(object sender, RoutedEventArgs e)
        {
            this.saveButton.IsEnabled = false;
            this.saveExitButton.IsEnabled = false;
            this.cancelButton.IsEnabled = false;

            //Model = _userFactory.Upsert(Model);
            await Task.Run(() => Model = _userService.Save(Model));


            this.saveButton.IsEnabled = true;
            this.saveExitButton.IsEnabled = true;
            this.cancelButton.IsEnabled = true;
        }

        private void saveExitButton_Click(object sender, RoutedEventArgs e)
        {
            saveButton_Click(sender, e);
            cancelButton_Click(sender, e);
        }

        private void cancelButton_Click(object sender, RoutedEventArgs e)
        {
            _userController.NavigateToListView();
        }
    }
}
